package ah.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


public class FileCopy {
	private static CPDetectorTest cpd = new CPDetectorTest();
	
	/**
	 * 
	 * @author dingdahai 日期: Jul 17, 2009 7:01:12 PM
	 * @param args
	 */
	public static void main(String[] args) {
		String workspace = "/home/ddh/workspace";
		String srcPath = workspace + "/MyTest/webapp/pages";
		String distPath = workspace + "/MyTest/webapp/pages2";
		
		FileCopy.copyFile("/home/ddh/workspace/MyTest/webapp/pages/javascript/table_上移下移.htm", "/home/ddh/workspace/MyTest/webapp/pages/javascript/table_上移下移2.htm","");
		FileCopy.copyFile("/home/ddh/workspace/MyTest/webapp/pages/javascript/table_上移下移.htm", "/home/ddh/workspace/MyTest/webapp/pages/javascript/table_上移下移5.htm","gb2312");
		FileCopy.copyFile("/home/ddh/workspace/MyTest/webapp/pages/javascript/table_上移下移.htm", "/home/ddh/workspace/MyTest/webapp/pages/javascript/table_上移下移6.htm","utf-8");
		FileCopy.copyFile("/home/ddh/workspace/MyTest/webapp/pages/javascript/table_上移下移.htm", "/home/ddh/workspace/MyTest/webapp/pages/javascript/table_上移下移3.htm","GBK");
		FileCopy.copyFile("/home/ddh/workspace/MyTest/webapp/pages/javascript/table_上移下移.htm", "/home/ddh/workspace/MyTest/webapp/pages/javascript/table_上移下移4.htm","gb18030");
		//FileCopy.delCVSDir(new File("E:/workspace/dianyiwang/WebRoot/WEB-INF/classes"));
		//FileCopy.createCVSIgnore(new File("E:/workspace/dianyiwang/WebRoot/WEB-INF/classes"));
		//FileCopy.clearFile(new File("E:/workspace/sv/WebRoot/upload/drug/.svn"));
		//copyDir(srcPath, distPath, new File(srcPath));
		//delCVSDir(new File("E:\\workspace\\tuanfei0518\\tuanfei"));
		
		//FileCopy.copyFile("src/number/NumberTest.java", "src/number/NumberTest3.java","GBK");
		
		
	}


	/**
	 * 
	 * @param dirFrom 如: F:\\jdk-1_5_0-src-jrl 使用系统的separator
	 * @param dirTo   如: F:\\jdk-1_5_0-src
	 */
	public static void copyDir(final String dirFrom, final String dirTo, File dir) {
		if (!dir.exists()) {
			return;
		}

		String filePath = dir.getAbsolutePath();
		filePath = filePath.replace(dirFrom, dirTo);

		File dirDist = new File(filePath);
		if (!dirDist.exists()) {
			dirDist.mkdirs();
		}

		File[] files = dir.listFiles(new MyFileFilter());
		for (File file : files) {
			if (file.isDirectory()) {
				copyDir(dirFrom, dirTo, file);
			} else {
				copyFile(dirFrom, dirTo, file);
			}
		}

	}

	// 复制文件,修改编码GBK
	public static void copyFile(final String fileFrom, final String fileTo, File file) {
		try {
			
			String filePath = file.getAbsolutePath();// F:\pic\time_bg.jpg
			filePath = filePath.replace(fileFrom, fileTo);
			
			String encode = cpd.method(file.getAbsolutePath());
			String encode2 = cpd.method2(file.getAbsolutePath());
			//排除不复制的情况
			if (encode == null ||  encode2 == null) {
				System.out.println(file.getAbsolutePath() + "\t编码: " + encode + ", " + encode2);
				return;
			} else if(!encode.equals(encode2)) {
				System.out.println(file.getAbsolutePath() + "\t编码: " + encode + ", " + encode2);
				return;
			} else if(!(encode.equalsIgnoreCase("GBK") 
					|| encode.equalsIgnoreCase("GB2312") 
					|| encode.equalsIgnoreCase("GB18030"))) {
				if (!(encode.equalsIgnoreCase("US-ASCII") 
					|| encode.equalsIgnoreCase("UTF-8")))
					System.out.println(file.getAbsolutePath() + "\t编码: " + encode + ", " + encode2);
				return;
			}
			
			FileInputStream fis = new FileInputStream(file);
			InputStreamReader isr = new InputStreamReader(fis, "GB18030");// , "GB18030"

			FileOutputStream fos = new FileOutputStream(new File(filePath));
			OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");

			char[] cbuf = new char[1024];
			int length = 0;
			while ((length = isr.read(cbuf)) > 0) {
				osw.write(cbuf, 0, length);
			}
			osw.close();
			fos.close();
			isr.close();
			fis.close();
			
			//cpd.method(filePath);
			//cpd.method2(filePath);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	/**
	 * 输出为utf-8编码文件
	 * @param srcFile 源文件
	 * @param distFile 目标文件
	 * @param encode 读取编码
	 */
	public static void copyFile(String srcFile, String distFile,String encode) {
		try {
			FileInputStream fis = new FileInputStream(srcFile);
			InputStreamReader isr = null;

			if (encode==null || "".equals(encode)) {
				isr = new InputStreamReader(fis);// , "GB18030"
			} else {
				isr = new InputStreamReader(fis, encode);
			}
			FileOutputStream fos = new FileOutputStream(distFile);
			OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");

			char[] cbuf = new char[1024];
			int length = 0;
			while ((length = isr.read(cbuf)) > 0) {
				osw.write(cbuf, 0, length);
			}
			osw.close();
			fos.close();
			isr.close();
			fis.close();
			
			CPDetectorTest cpd = new CPDetectorTest();
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	/**
	 * 返回文件内容
	 * @author dingdahai
	 * @date Nov 18, 2009 10:56:19 AM
	 * @param str
	 * @return
	 */
	public static String readFileStr(String fileStr) {
		StringBuilder stringBuilder = new StringBuilder();
		try {
			FileInputStream fis = new FileInputStream(fileStr);
			InputStreamReader isr = new InputStreamReader(fis);

			char[] cbuf = new char[1024];
			int length = 0;
			while ((length = isr.read(cbuf)) > 0) {
				stringBuilder.append(new String(cbuf, 0, length));
			}
			isr.close();
			fis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return stringBuilder.toString();
	}
	
	/**
	 * 返回文件内容
	 * @author dingdahai
	 * @date Nov 18, 2009 10:56:19 AM
	 * @param str
	 * @return
	 */
	public static byte[] readFileBytes(String fileStr) {
		StringBuilder stringBuilder = new StringBuilder();
		byte[] b = null;
		try {
			FileInputStream fis = new FileInputStream(fileStr);

			b = new byte[fis.available()];
			fis.read(b);
			fis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return b;
	}
	
	/**
	 * 写文件 utf8
	 * @param fileName
	 * @param content
	 */
	public static void writeFile(String fileName, String content) {
		try {
			FileOutputStream fos = new FileOutputStream(new File(fileName));
			OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
			
			osw.write(content);
			osw.close();
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除空目录
	 * @author dingdahai
	 * Jan 11, 2010 2:47:06 PM
	 * @param dir
	 */
	public static void delEmptyDir(File dir) {
		File[] files = dir.listFiles();
		if(files!=null && files.length>0){
			for(File file : files){
				if (file.isDirectory()) {
					delEmptyDir(file);
				}
			}
		}
		//处理完子目录后再判断是否为空目录
		files = dir.listFiles();
		if(files==null || files.length<1){
			dir.delete();
		}
		
	}
	/**
	 * 删除CVS目录
	 * @author dingdahai
	 * @param dir
	 */
	public static void delCVSDir(File dir) {
		File[] files = dir.listFiles();
		if(files!=null && files.length>0){
			for(File file : files){
				if (file.isDirectory()) {
					if (file.getName().equals("CVS")) {
						System.out.print(file.getAbsolutePath() + "\n");
						FileCopy.clearFile(file);
						file.delete();
					} else {
						delCVSDir(file);
					}
				}
			}
		}
	}
	
	/**
	 * 在dir目录和每个子目录下创建.cvsignore文件
	 * @param dir
	 */
	public static void createCVSIgnore(File dir) {
		if (dir.isDirectory()) {
			if (!dir.getName().equals("CVS")) {//忽略cvs目录
				try {
					FileWriter fw = new FileWriter(new File(dir, ".cvsignore"));
					fw.write("*.class\r\n");
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} else {
			return;
		}
		File[] files = dir.listFiles();
		if(files!=null && files.length>0){
			for(File file : files){
				if (file.isDirectory()) {
					FileCopy.createCVSIgnore(file);
				}
			}
		}
	}
	
	
	/**
	 * 清空目录或删除文件
	 * @param dir
	 */
	public static void clearFile(File dir) {
		if (dir.isFile()) {
			dir.delete();
			return;
		}
		File[] files = dir.listFiles();
		if (files == null || files.length<1) {
			dir.delete();
		}
		for (File file : files) {
			FileCopy.clearFile(file);
		}
	}
}
